package shopping.web;

import com.mysql.cj.x.protobuf.MysqlxCrud;
import shopping.bean.*;
import shopping.service.OrderService;
import shopping.service.impl.OrderServiceImpl;
import shopping.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

public class OrderServlet extends BaseServlet{

    private OrderService orderService = new OrderServiceImpl();
    public void selectOrderAll(HttpServletRequest req,HttpServletResponse resp){
        Admin loginAdmin = (Admin) req.getSession().getAttribute("admin");
        Integer adminId = loginAdmin.getId();
        ArrayList<Order> orderList = orderService.selectOrderAll();
//        System.out.println(orderList);
        //保存orderList对象到Request域中
        req.setAttribute("orderList",orderList);
        //请求转发到/view/manager/order_manager.jsp页面
        try {
            req.getRequestDispatcher("/view/manager/order_manager.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void selectOrderItemByOrder_id(HttpServletRequest req,HttpServletResponse resp){
//        Admin loginAdmin = (Admin) req.getSession().getAttribute("admin");
//        Integer adminId = loginAdmin.getId();
        String order_id = req.getParameter("order_id");

        ArrayList<OrderItem> orderItems = orderService.selectOrderItemByOrder_id(order_id);
//        System.out.println(orderList);
        //保存orderList对象到Request域中
        req.setAttribute("orderItems",orderItems);
        //请求转发到/view/manager/order_manager.jsp页面
        try {
            req.getRequestDispatcher("/view/manager/order_manager_detail.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 生成订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int status = WebUtils.parseInt(req.getParameter("status"), 0);
        // 获取Adminid
        Admin loginAdmin = (Admin) req.getSession().getAttribute("admin");
        Integer adminId = loginAdmin.getId();
//        调用orderService.createOrder(List,Adminid);生成订单
        String orderId = null;
        if (status == 0){
            // 先获取List购物车对象
            List cl_list = (List) req.getSession().getAttribute("cl_list");

//        try {
            orderId = orderService.createOrder(cl_list, adminId,status);
//            JdbcUtils.commitAndClose();//提交事务并释放连接
//        } catch (Exception e) {
//            JdbcUtils.rollbackAndClose();//回滚事务并释放连接
//            e.printStackTrace();
//        }
        }else {
            // 先获取List购物车对象
            List go_list = (List) req.getSession().getAttribute("go_list");
//        try {
            orderId = orderService.createOrder(go_list, adminId,status);
//            JdbcUtils.commitAndClose();//提交事务并释放连接
//        } catch (Exception e) {
//            JdbcUtils.rollbackAndClose();//回滚事务并释放连接
//            e.printStackTrace();
//        }
        }
        //如果用户没有登录，必须先登录
        if (loginAdmin == null) {
            req.getRequestDispatcher("/view/admin/login.jsp").forward(req,resp);
            return;
        }



//        req.setAttribute("orderId", orderId);
        // 请求转发到/view/list/listout.jsp
//        req.getRequestDispatcher("/view/list/listout.jsp").forward(req, resp);

        req.getSession().setAttribute("orderId",orderId);

        //改用重定向，防止不断刷新导致表单重复提交
        resp.sendRedirect(req.getContextPath()+"/view/list/listout.jsp");
    }

}
